package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.entity.SelectMediaEntity;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.LocalVideoAdapter;
import flc.ast.adapter.VideoAlbumAdapter;
import flc.ast.bean.MyAlbumBean;
import flc.ast.databinding.ActivityVideoAlbumBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.view.container.StkRelativeLayout;

public class VideoAlbumActivity extends BaseAc<ActivityVideoAlbumBinding> {

    public static String albumId;
    private LocalVideoAdapter videoAdapter;
    private List<SelectMediaEntity> listShow = new ArrayList<>();
    private Dialog myDeleteDialog;


    @Override
    protected int onCreate() {
        return R.layout.activity_video_album;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivVideoAlbumBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivAdd.setOnClickListener(this);
        mDataBinding.ivDelete.setOnClickListener(this);

        mDataBinding.rvVideoList.setLayoutManager(new GridLayoutManager(mContext, 2));
        videoAdapter = new LocalVideoAdapter();
        mDataBinding.rvVideoList.setAdapter(videoAdapter);
        videoAdapter.setOnItemClickListener(this);

        //删除弹框
        deleteDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAdd://添加视频
                startActivity(AddVideoActivity.class);
                break;
            case R.id.ivDelete://删除相册
                myDeleteDialog.show();
                break;
            //删除弹框
            case R.id.ivDialogDeleteAlbumConfirm:
                delete();
                myDeleteDialog.dismiss();
                break;
            case R.id.ivDialogDeleteAlbumCancel:
                myDeleteDialog.dismiss();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        PlayActivity.listPlay = listShow;
        PlayActivity.type = 1;
        PlayActivity.selPlayPos = position;
        startActivity(PlayActivity.class);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        List<MyAlbumBean> listSp = AppSpUtils.getAlbumList();
        if (listSp != null && listSp.size() > 0) {
            for (MyAlbumBean bean : listSp) {
                if (bean.getId().equals(albumId)) {
                    mDataBinding.tvVideoAlbumTitle.setText(bean.getName());
                    listShow.addAll(bean.getList());
                    break;
                }
            }
        }
        if (listShow != null && listShow.size() > 0) {
            videoAdapter.setList(listShow);
            mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
            mDataBinding.tvVideoNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvVideoList.setVisibility(View.GONE);
            mDataBinding.tvVideoNoData.setVisibility(View.VISIBLE);
        }
    }

    //删除相册
    private void delete() {
        List<MyAlbumBean> listSp = AppSpUtils.getAlbumList();
        if (listSp != null && listSp.size() > 0) {
            for (int i = 0; i < listSp.size(); i++) {
                if (listSp.get(i).getId().equals(albumId)) {
                    listSp.remove(i);
                    break;
                }
            }
            AppSpUtils.saveAlbumList(listSp);
            ToastUtils.showShort(getString(R.string.delete_suc));
        }
        finish();
    }

    //删除弹框
    private void deleteDialog() {
        myDeleteDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete_album, null);
        myDeleteDialog.setContentView(inflate);
        myDeleteDialog.setCancelable(true);
        Window dialogWindow = myDeleteDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView right = inflate.findViewById(R.id.ivDialogDeleteAlbumConfirm);
        ImageView cancel = inflate.findViewById(R.id.ivDialogDeleteAlbumCancel);
        right.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

}